package org.vectrics.recruiter.struts.admin;

import java.util.List;

import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Form;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.view.FieldTable;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.JobCandidateCriteria;
import org.vectrics.recruiter.struts.ButtonUtils;
import org.vectrics.recruiter.struts.tag.CandidateJobsTable;
import org.vectrics.recruiter.struts.tag.EmailedResumeAttachmentListTag;
import org.vectrics.recruiter.user.resume.email.EmailedResumeInfo;
import org.vectrics.struts.VectricsTagSupport;
import org.vectrics.user.Person;


public class EmailedResumeSummaryTag extends VectricsTagSupport {

	private static final long serialVersionUID = 1602376119278870619L;
	
	private String emailedResumeId = null;
	private Table buttonTable = new Table();

	public EmailedResumeSummaryTag() {
		super();
	}
	
	private void addButtons(EmailedResumeInfo emailedResumeInfo) {
		if (emailedResumeInfo.getPerson() != null) {
			Form button = this.addButtonRow(emailedResumeInfo, 
					"/hrs-intranet/admin/user/view.do", 
						"button.person.view", "view_prsn_form");
			button.addHidden("userId", emailedResumeInfo.getPerson().getId().toString());
/*		
			button = this.addButtonRow(emailedResumeInfo, 
					"/hrs-intranet/admin/jobs/emailedResumes/reassignPerson.do", 
						"button.email.resume.reassign");
			button.addHidden("emailResumeInfoId", emailedResumeInfo.getId().toString());
*/
		}
	}

	
	private Form addButtonRow(EmailedResumeInfo emailedResumeInfo, String href, String captionCode, String formId) {
		Row row = buttonTable.addRow();
		Cell cell = row.addCell();
		Form button = ButtonUtils.createButtonForm(this.getRequest(), href, captionCode, this.getLocale(), formId, "submit", "submit");
		cell.addElement(button.toString());
		return(button);
	}

	
	protected String makeStartTag() {
		StringBuffer html = new StringBuffer();
		EmailedResumeInfo emailedResumeInfo = RecruitServiceLocator.getRecruitUserService().findEmailedResume(new Long(this.getEmailedResumeId()));
		Table mainTable = new Table();
		Row mainRow = mainTable.addRow();
		Cell mainLeftCell = mainRow.addCell();
		mainLeftCell.setVAlign("top");
		Cell mainRightCell = mainRow.addCell();
		mainRightCell.setVAlign("top");
		mainRightCell.addElement(this.buttonTable);
		addButtons(emailedResumeInfo);
		
        FieldTable fieldTable = new FieldTable();
        fieldTable.setCellPadding(2);
        fieldTable.setWidth(500);
        fieldTable.setValueCellWidth("350");
    	Person person = emailedResumeInfo.getPerson(); 
        if (person != null)
        	fieldTable.showField(
        			this.getPhrase("prompt.person.name"), person.getName());
        else 
        	fieldTable.showField(
        			this.getPhrase("prompt.person.name"), "Not Assigned");

        fieldTable.showField(this.getPhrase(
        		"prompt.email.subject"), emailedResumeInfo.getEmailSubject());
        fieldTable.showField(
        		this.getPhrase("prompt.emailed.resume.status"), emailedResumeInfo.getStatusCode());
        
    	Table attachmentTable = EmailedResumeAttachmentListTag.createAttachmentListTable(
    			this.getRequest(), emailedResumeInfo, true, true); 
    	fieldTable.showFieldElement(this.getPhrase("prompt.emailed.resume.attachment"), attachmentTable);
        
        mainLeftCell.addElement(fieldTable);
		html.append(mainTable.toString());
		
		if (person != null) {
			JobCandidateCriteria criteria = new JobCandidateCriteria();
			criteria.setPerson(person);
			html.append("<h3>" + this.getPhrase("heading.candidate.jobs") + "</h3>");
			List jobCandidates = RecruitServiceLocator.getJobProvider().findJobCandidates(criteria, 0, 100);
			CandidateJobsTable candidateJobsTable = new CandidateJobsTable(this.getAuthPerson(), this.getRequest(), jobCandidates);
			html.append(candidateJobsTable.toString());
		}
		return(html.toString());
	}

	
	/**
	 * @return Returns the emailedResumeId.
	 */
	public String getEmailedResumeId() {
		return emailedResumeId;
	}

	/**
	 * @param emailedResumeId The emailedResumeId to set.
	 */
	public void setEmailedResumeId(String emailedResumeId) {
		this.emailedResumeId = emailedResumeId;
	}

}
